﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using System.Text;
using SDT.Lib.Mail;
using SDT.Site.Properties;
using SDT.Site.Models;

namespace ADR.MAG.Controllers
{
    public class AjaxDataController : Controller
    {
        //
        // GET: /AjaxData/

        SDTDataContext eDataContext = new SDTDataContext();
        public ActionResult GetDistrict(int id)
        {
            try
            {
                var dic = eDataContext.Districts.Where(d => d.CityID == id).ToList();
                return View(dic);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetProductCategoryByType(int id)
        {
            try
            {
                List<ProductCategory> proCat = eDataContext.ProductCategories
                                                .Where(c => c.ActiveYN.HasValue && c.ActiveYN.Value == true)
                                                .ToList();
                return View(proCat);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetPrice(int id)
        {
            try
            {
                List<ConvertPrice> p = eDataContext.ConvertPrices
                                                .ToList();
                return View(p);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        
        

        /// <summary>
        /// Author: Takamaru Nguyen.
        /// Date: 03/08/2011.
        /// Description: Update ndownload when users click download document witdh id.
        /// </summary>
        public void UpdateDownload(int id)
        {
            try
            {
                Document doc = eDataContext.Documents.Where(d => d.ID == id).FirstOrDefault();
                doc.nDownload = doc.nDownload + 1;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        
        public JsonResult SearchTag()
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var term = Request["keyword"].ToString();
                var keywords = eDataContext.Tags
                    .Where(e => e.Keyword.IndexOf(term) > -1)
                    .Take(10)
                    .ToList();
                return Json(keywords, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        public CaptchaImageResult ShowCaptchaImage()
        {
            return new CaptchaImageResult();
        }
        public JsonResult ValidateCatcha()
        {
            try
            {
                if (Request["CaptchaText"].ToLower() == HttpContext.Session["captchastring"].ToString().ToLower())
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
        
        [HttpPost]
        public ActionResult LoadDistrictByCity(int id)
        {
            try
            {
                var dic = eDataContext.Districts.Where(d => d.CityID == id).ToList();
                return View("ucLoadDistrictByCity", dic);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public JsonResult BannerClick(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var b = eDataContext.Banners.Where(e => e.ID == id).SingleOrDefault();
                b.nClick += 1;
                eDataContext.SubmitChanges();
                return Json("", JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult ValidateEmail()
        {
            try
            {
                var email = Request["Customer.Email"];
                return Json(!CustomerManager.CheckByEmail(email), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
        public JsonResult ValidateUserName()
        {
            try
            {
                var userName = Request["Customer.UserName"];
                return Json(!CustomerManager.CheckUserName(userName), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
    }
}
